<?php

namespace App\Http\Controllers;

use App\Http\Traits\UserTrait;
use App\Models\User;
use Auth;
use Laravel\Socialite\Facades\Socialite;

class GitLabController extends Controller
{
    use UserTrait;

    public function callback()
    {
        $user = Socialite::with('gitlab')->user();

        /** @var User $user_info */
        $user_info = $this->touchUser($user->user, $user->token);
        $this->loginUsingId($user_info->id);

        return redirect()->route('home');
    }

    public function auth()
    {
        $client_id                  = config('services.gitlab.client_id');
        $client_secret              = config('services.gitlab.client_secret');
        $redirect_url               = config('services.gitlab.redirect');
        $instance_uri               = config('services.gitlab.instance_uri');
        $additional_provider_config = ['instance_uri' => $instance_uri];
        $config                     = new \SocialiteProviders\Manager\Config($client_id, $client_secret, $redirect_url, $additional_provider_config);

        return Socialite::with('gitlab')->setConfig($config)->redirect();
    }
}
